<?php
/**
 * 发送邮件
 * @author chenjunliang@star-net.cn
 * @copyright  ktvme. All Rights Reserved.
 * @version $Id: Email.php 592 2018-04-10 12:41:04Z chendanming $
 */

namespace app\console\swooletask\logic;


use email\PHPMailer;
use think\Log;

class Email {

    /**
     * 发送邮件
     * @param $email
     * @return array
     */
    public function sendEmail($email){
        Log::record(json_encode(['send_mail'=>$email]), 'elk_more_info','email');
        //Log::save();
        $subject = $email['subject'];
        $from    = 'K米广告';//环境描述
        if(ENVIRONMENT_MODE != 'production') {
            $subject = 'env_' . ENVIRONMENT_MODE . '|' . $subject;
            $from    = '测试_' . $from;
        }
        $mail = new PHPMailer();

        $mail->IsSMTP();
        $mail->CharSet = 'utf-8';
        foreach($email['reciever'] as $value) {
            $mail->AddAddress($value);
        }
        $mail->Body     = $email['content'];
        $mail->From     = $email['sender'];
        $mail->FromName = $from;
        $mail->Subject  = $subject;
        $mail->Host     = 'mail.star-net.cn';
        $mail->SMTPAuth = true;
        $mail->Username = $email['sender']; //发件人
        $mail->Password = $email['password'];//密码
        $mail->IsHTML(true);        //改成HTML格式
        $status = $mail->Send();
        Log::record(json_encode(['send_mail'=>json_encode($status)]), 'elk_more_info','email');
        //Log::save();
        return ['type'=>'email','status'=>$status,'subject'=>$subject,'content'=>$email['content'],'sender' => $email['sender']];
    }
}